from anole.core.processor import HeadProcessor
import urlparse
from anole import AnoleLog
class Redirector(HeadProcessor):
	_CODES=[301,302,303]
	def __init__(self,pm):
		self.__usp=pm.get('USP')
		self.__log=AnoleLog().log
	def process_head(self,head,req):
		new=head['location']
		new=urlparse.urljoin(req.strUrl,new)	
		self.__log('redirect to new url:'+new,AnoleLog.LEVEL_INFO)
		uObj=req.uObj
		try:
			ourl=uObj.ourl
		except AttributeError:
			ourl=uObj.strUrl
		uObj.ourl=ourl#original url
		uObj.strUrl=new
		self.__usp.put_url(uObj)
